package com.example.certificate.controller;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.certificate.annoatiton.CurrentUser;
import com.example.certificate.annoatiton.LoginToken;
import com.example.certificate.annoatiton.PassToken;
import com.example.certificate.dto.BasePageDTO;
import com.example.certificate.dto.OrderCarDTO;
import com.example.certificate.dto.OrderIinsDto;
import com.example.certificate.dto.UpdateStatusDTO;
import com.example.certificate.dto.common.DelByIdDto;
import com.example.certificate.entity.OrderCart;
import com.example.certificate.entity.OrderFrom;
import com.example.certificate.entity.ProductInfo;
import com.example.certificate.entity.User;
import com.example.certificate.mapper.CustomerAddrMapper;
import com.example.certificate.mapper.OrderFromMapper;
import com.example.certificate.service.OrderFromService;
import com.example.certificate.utils.ResultEntity;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hao
 * @since 2022-03-10 14:03:12
 */
@RestController
@RequestMapping("/domain/orderFrom")
public class OrderFromController {

    @Autowired
    private OrderFromService fromService;

    @Resource
    private OrderFromMapper orderFromMapper;

    @LoginToken
    @ApiOperation(value = "新增订单")
    @PostMapping(value = "/insertOrder")
    public ResultEntity insertOrder(@RequestBody OrderIinsDto dto, @Ignore @CurrentUser User user){
        return fromService.insertOrder(dto,user);
    }


    @LoginToken
    @ApiOperation(value = "已发货")
    @PostMapping(value = "/deliverGoods")
    public ResultEntity deliverGoods(@Validated(value = {DelByIdDto.delDataById.class}) @RequestBody DelByIdDto dto){
        OrderFrom orderFrom = orderFromMapper.selectById(dto.getId());
        orderFrom.setStatus(2);
        orderFromMapper.updateById(orderFrom);
        return new ResultEntity();
    }

    @LoginToken
    @PostMapping(value = "/updateStus")
    public ResultEntity updateStus(@RequestBody UpdateStatusDTO dto){
        return fromService.updateStus(dto);
    }

    @LoginToken
    @PostMapping("/myOrderFrom")
    @ApiOperation("查询我的订单")
    public ResultEntity selCar(@RequestBody BasePageDTO dto, @ApiIgnore @CurrentUser User user){

        Page<OrderFrom> page=new Page<>();
        page.setCurrent(dto.getCurrent()==null?1:dto.getCurrent());
        page.setSize(dto.getPageSize()==null?10:dto.getPageSize());

        dto.setUserId(user.getId());

        List<OrderFrom> orderFroms = orderFromMapper.selOrderPage(page, dto, null);

        if(!CollectionUtils.isEmpty(orderFroms)){
            Iterator<OrderFrom> iterator = orderFroms.iterator();
            while (iterator.hasNext()){
                OrderFrom next = iterator.next();
                next.setLists(JSONArray.parseArray(next.getDetailDesc(), ProductInfo.class));
            }
        }

        return new ResultEntity(page.setRecords(orderFroms));
    }

    @PassToken
    @PostMapping("/myOrderFromDetail")
    @ApiOperation("查询我的订单详情")
    public ResultEntity selCar(@RequestBody OrderCarDTO dto){

        OrderFrom orderFroms = orderFromMapper.selectById(dto.getProductId());
        orderFroms.setLists(JSONArray.parseArray(orderFroms.getDetailDesc(), ProductInfo.class));

        return new ResultEntity(orderFroms);
    }

    @LoginToken
    @ApiOperation(value = "分页查询订单")
    @PostMapping(value = "/selPages")
    public ResultEntity selPages(@RequestBody BasePageDTO dto, @CurrentUser @Ignore User user){

        Page<OrderFrom> page=new Page<>();
        page.setCurrent(dto.getCurrent() ==null ? 1 : dto.getCurrent());
        page.setSize(dto.getPageSize() ==null ? 10 : dto.getPageSize());

        List<OrderFrom> orderFroms = new ArrayList<>();

        if(user.getRoleId().equals("1703675027755974657")){
            orderFroms = orderFromMapper.selOrderPage(page, dto, null);
        }else {
            orderFroms = orderFromMapper.selOrderPage(page, dto, user.getId());
        }

        if(!CollectionUtils.isEmpty(orderFroms)){

            for (OrderFrom next:
                    orderFroms) {
                next.setLists(JSONArray.parseArray(next.getDetailDesc(), ProductInfo.class));
            }
        }

        return new ResultEntity(page.setRecords(orderFroms));
    }


}

